package com.onemore.task;

import java.sql.SQLException;

import org.voovan.http.server.HttpRequest;
import org.voovan.http.server.HttpResponse;
import org.voovan.test.tools.json.JSONDecodeTest;
import org.voovan.tools.json.JSONDecode;
import org.voovan.tools.log.Logger;

import com.alibaba.druid.support.json.JSONParser;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.onemore.task.model.ModelJorder;
import com.onemore.task.model.ModelJordermx;
import com.onemore.task.model.ModelJsend;
import com.onemore.task.model.ModelJsendmx;
import com.onemore.task.model.ModelShangpin;
import com.onemore.task.model.ModelSpgg1;
import com.onemore.task.model.ModelSpgg2;
import com.onemore.task.remote.TaskGoods;
import com.onemore.task.remote.TaskJsend;
import com.onemore.task.remote.TaskJorder;
import com.onemore.task.remote.RmtTaskBase;
import com.onemore.task.remote.TaskTest;
import com.onemore.task.remote.TaskVendor;

/**
 * 协议接收并处理业务  默认编码格式UTF-8
 * @author zzj
 *
 * Voovan Framework.
 */
public class Onemore extends RmtTaskBase{
//	String message = "未设置";
//    private final String charset = "UTF-8";
	public Onemore() {
		
	}
	public void disposeSubmit(HttpRequest request, HttpResponse response)
	{
		String app_act = request.getParameter("app_act");
		
		String content = request.body().getBodyString();//request.getParameter("content");
        if(content == null) {
			message = "Body不能为空";
	        putResult(response, -460, message);
        	return;
        }
        if(content.length() < 2) {
			message = "Body长度异常";
	        putResult(response, -461, message);
        	return;
        }
		Logger.info("app_act:"+app_act+" content:"+content);
		JSONParser parse =new JSONParser(content);  //创建json解析器
        JSONObject jsonObject= new JSONObject(parse.parseMap());
        message = "成功";
        if(app_act == null) {
			message = "没有找到app_act这个参数";
	        putResult(response, -401, message);
        	return;
        }

		if(jsonObject == null) {
			message = "app_act="+app_act+" 接收到的数据，转换为json失败";
    		Logger.error(message);
	        putResult(response, -402, message);
			return;
    	}
        if(app_act.startsWith("test")) {
    		TaskTest task = new TaskTest();
    		task.process(jsonObject, response);
		}else
		if(app_act.startsWith("vendor")) {
    		TaskVendor taskVendor = new TaskVendor();
    		taskVendor.process(jsonObject, response);
		}else
		if(app_act.startsWith("goods")) {
    		TaskGoods task = new TaskGoods();
    		task.process(jsonObject, response);
		}else
		if(app_act.startsWith("jsend")) {
    		TaskJsend task = new TaskJsend();
    		task.process(jsonObject, response);
		}else
		if(app_act.startsWith("jorder")) {
    		TaskJorder task = new TaskJorder();
    		task.process(jsonObject, response);
		}else {
	        putResult(response, -380, "没有找到app_act="+app_act+"参数对应的接口");
        	return;
		}
		return;
	}
	/**
	 * 向服务器返回结果
	 * @param response
	 * @param ret
	 * @param message
	 * @return
	 */
	/*public boolean putResult(HttpResponse response,int ret,String message) {
		JSONObject json = new JSONObject();
		json.put("flag", (ret==0||ret==200)?"success":"failure");
		json.put("code", ret);
		json.put("message", message);
		String str = json.toJSONString();
		response.header().put("Content-Rangle", "bytes " + 0 + "-" + (str.length()));
		response.header().put("Content-Type", "text/html;charset="+charset);
		response.body().write(str);
		return true;
	}*/
	
	/*public void db_test(HttpRequest request, HttpResponse response)
	{
		boolean ret = true;
		String urlPath = request.protocol().getPath();
		String val1 = request.getParameter("bbbb");
		String val2 = request.getParameter("json");
		String val3 = request.getParameter("ccc");
		Logger.info("bbbb:"+val1+" json:"+val2+" ccc:"+val3+" url:"+urlPath);
		response.header().put("Content-Range", "bytes " + 10 + "/" + 10);
		response.header().put("Content-Type", "text/html;charset="+charset);
		response.body().write(val2);
	}*/
}
